// pages/myAfterSale/myAfterSale.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listItem: [],
    total:"",
    files: [],
    filesImg: [],
    content:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    
    var order=JSON.parse(options.item)
    console.log(order)
    that.setData({
      orderData:order,
      total: (order.goods.goods_price)*(order.num)
    })
    wx.getStorage({
      key: 'token',
      success: function(res) {
        token=res.data
      },
    })
  },
  content:function(e){
    this.setData({
      content:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        console.log(res)
        var uploudImg = res.tempFilePaths[0];


        wx.uploadFile({
          url: url.URl + 'file/upload',
          filePath: uploudImg,
          name: 'file',
          formData: { type: 1 },
          header: {
            "Content-Type": "multipart/form-data",
            'Authorization': token
          },
          success: function (res) {
            console.log(res)
            var data = JSON.parse(res.data)
            console.log(data)
            console.log(url)
            var newwImg = url.URl2 + data.data
            console.log(newwImg)
            if (data.code == 200) {
              wx.showToast({
                title: '图片上传成功',
              })
              that.setData({
                files: that.data.files.concat(newwImg),
                filesImg: that.data.files.concat(data.data),
              })
            } else {

            }

          },
          fail: function (res) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  save: function () {
    var that = this;
    var content1 = that.data.content;
    var filesImg = that.data.filesImg;
    var orderData = that.data.orderData;
    if (content1.length == 0) {
      wx.showToast({
        title: '请填写退款原因',
        icon: "none"
      })
    } else if (filesImg.length == 0) {
      wx.showToast({
        title: '请上传凭证',
        icon: "none"
      })
    } else {
      var data = {};
      data.id = orderData.id;
      data.content = content1;
      data.images = filesImg.join(",");

      console.log(data)
      wx.request({
        url: url.URl + 'my/treasure/refund',
        data: data,
        method: 'post',
        header: {
          'Authorization': token
        },
        success: function (res) {
          var msg = res.data.msg
          wx.showToast({
            title: msg,
            icon: 'none',
            success:function(){
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        }, fail: function () {
          wx.showToast({
            title: '请检查您的网络',
            icon: 'none',
          })
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})